package com.yami.shop.metric.controller;

import com.alibaba.fastjson.JSONObject;
import com.yami.shop.bean.app.dto.OrderCountData;
import com.yami.shop.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.Date;


/**
 * 指标服务
 *
 * @author: fangzong.lyj@alibaba-inc.com
 * @date: 2022/12/09 18:36
 */
@RestController
@RequestMapping("/metric")
@Api(tags = "指标")
public class MetricController {

    @Autowired
    private OrderService orderService;

//
//    @Operation(summary = "查询 Addon 列表")
//    @GetMapping
//    public JSONObject get() {
//        JSONObject metrics = new JSONObject();
//        int max = 200;
//        int min = 20;
//        int total_cnt = min + (int)(Math.random() * (max - min + 1));
//
//        int s_total_cnt = min + (int)(Math.random() * (total_cnt - min + 1));
//
//        metrics.put("total_order_cnt_per_minute", total_cnt);
//        metrics.put("success_total_order_cnt_per_minute", s_total_cnt);
//        metrics.put("failed_total_order_cnt_per_minute", total_cnt - s_total_cnt);
//        return metrics;
//    }

    @Operation(summary = "查询 Addon 列表")
    @GetMapping
    public JSONObject get() {
        JSONObject metrics = new JSONObject();

        Calendar cal = Calendar.getInstance();
       Date date = new Date();
        cal.setTime(date);
        cal.add(Calendar.MINUTE, -1);
        OrderCountData orderCountData = orderService.getOrderCountByTime(cal.getTime());

        int total = orderCountData.getAllCount();
        int success = orderCountData.getSuccess();
        int open = total - success;

        JSONObject data = new JSONObject();
        data.put("value", open);
        data.put("metric_id", 50);
        data.put("ts", date.getTime()/1000);

        JSONObject labels = new JSONObject();
        labels.put("app_id", "mall4j");
        labels.put("app_name", "mall4j");
        data.put("labels", labels);

        metrics.put("data", data);
        return metrics;
    }
}
